package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TProdTotal;
import com.ruoyi.system.service.IMainService;
import com.ruoyi.system.service.ITProdTotalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author wux
 * @date 2023/5/23 11:28
 */
@Api(tags = "(客户端) 产品生产目标")
@RestController
@RequestMapping("/client/prod/total")
public class TProdTotalController {
    @Autowired
    private ITProdTotalService prodTotalService;
    @Autowired
    private IMainService mainService;

    @PostMapping("/add")
    @ApiOperation(value = "新增/修改生产目标")
    public ResultEntity add(@Valid @RequestBody TProdTotal params) {
        if(Objects.isNull(params.getId())){
            params.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        }
        params.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        prodTotalService.saveOrUpdate(params);
        mainService.countProjectYearProd(params.getProjectId());
        mainService.countProjectMonthProd(params.getProjectId());
        mainService.countProjectDayProd(params.getProjectId());
        return ResultEntity.createSuccess();
    }

    @GetMapping("/query/{projectId}")
    @ApiOperation(value = "查询总生产目标")
    public ResultEntity query(@PathVariable("projectId") Long projectId) {
        return ResultEntity.createSuccess(prodTotalService.getOne(new LambdaQueryWrapper<TProdTotal>().eq(TProdTotal::getProjectId,projectId)));
    }

}
